package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.SmsFlashPromotionProductRelation;
import com.itheima.domain.SmsFlashPromotionSession;
import com.itheima.mapper.SmsFlashPromotionMapper;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.mapper.SmsFlashPromotionSessionMapper;
import com.itheima.service.SmsFlashPromotionProductRelationService;
import com.itheima.service.SmsFlashPromotionSessionService;
import com.itheima.vo.SessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 限时购场次表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements SmsFlashPromotionSessionService {

    @Autowired
    SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;
    @Autowired
    SmsFlashPromotionMapper smsFlashPromotionMapper;

    @Override
    public List<SessionVo> selectListByid(Integer flashPromotionId) {
        if (smsFlashPromotionMapper.selectById(flashPromotionId).getStatus() != 1) {
            return null;
        }
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        queryWrapper.groupBy(SmsFlashPromotionProductRelation::getFlashPromotionSessionId);
        List<SmsFlashPromotionProductRelation> relationList = smsFlashPromotionProductRelationMapper.selectList(queryWrapper);
        //遍历关系集合，获取每一个时刻id
        List<SessionVo> list = new ArrayList<>();
        if (ArrayUtil.isNotEmpty(relationList)) {
            for (SmsFlashPromotionProductRelation relation : relationList) {
                LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, relation.getFlashPromotionSessionId());
                Integer count = smsFlashPromotionProductRelationMapper.selectCount(queryWrapper1);
                SessionVo sessionVo = new SessionVo();
                LambdaQueryWrapper<SmsFlashPromotionSession> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(SmsFlashPromotionSession::getId,relation.getFlashPromotionSessionId());
                queryWrapper2.eq(SmsFlashPromotionSession::getStatus,1);
                SmsFlashPromotionSession session = this.getOne(queryWrapper2);
                if (session != null){
                    BeanUtil.copyProperties(session,sessionVo);
                    sessionVo.setProductCount(count);
                    list.add(sessionVo);
                }
            }
        }
        return list;
    }
}
